package main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
 * selects the tech
 * @author Bart Machielse and Justin Yu
 *
 */
public class TechSelector {
	Player p;
	TechTree tech;
	JFrame f = new JFrame("technology is amazing, mate");
	JPanel pane = new JPanel();
	JButton war = new JButton("War");
	JButton metal = new JButton("Metal");
	JButton tanks = new JButton("Tanks");
	JButton can = new JButton("Cannon");
	JButton horse = new JButton("Horses");
	JButton econ = new JButton("Econ");
	JButton resef = new JButton("Resource Efficiency");
	JButton foo = new JButton("Food");
	JButton stone = new JButton("Stones");
	JButton wood = new JButton("Wood");
	JButton culture = new JButton("Culture");
	String newline = System.getProperty("line.separator");

	int MetalL[] = new int[3];
/**
 * base constructor
 * @param p1 the player owner
 */
	public TechSelector(Player p1) {
		p = p1;
		tech = p.getRes().getTree();

		f.add(pane);

		if(tech.selectTech("MetalW")) {
			MetalL[0] = 1;
			pane.add(metal);
		} else if(tech.selectTech("MetalW2")) {
			MetalL[1] = 1;
			pane.add(metal);
		} else if(tech.selectTech("MetalW3")) {
			MetalL[2] = 1;
			pane.add(metal);
		}

		if(tech.selectTech("War2") || tech.selectTech("War3"))
			pane.add(war);
		if(tech.selectTech("Tanks"))
			pane.add(tanks);
		if(tech.selectTech("Can"))
			pane.add(can);
		if(tech.selectTech("Horse"))
			pane.add(horse);
		if(tech.selectTech("Econ2") || tech.selectTech("Econ3"))
			pane.add(econ);
		if(tech.selectTech("Resef1") || tech.selectTech("Resef2") || tech.selectTech("Resef3"))
			pane.add(resef);
		if(tech.selectTech("FoodBonus1") || tech.selectTech("FoodBonus2"))
			pane.add(foo);
		if(tech.selectTech("WoodBonus1") || tech.selectTech("WoodBonus2"))
			pane.add(wood);
		if(tech.selectTech("StoneBonus1") || tech.selectTech("StoneBonus2"))
			pane.add(stone);
		if(tech.selectTech("Points"))
			pane.add(culture);

		f.setSize(350, 200);
		f.setVisible(true);

		foo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(tech.selectTech("FoodBonus1"))
					selectTech("FoodBonus1");
				else
					selectTech("FoodBonus2");
			}
		});

		wood.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(tech.selectTech("WoodBonus1"))
					selectTech("WoodBonus1");
				else
					selectTech("WoodBonus2");
			}
		});

		stone.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(tech.selectTech("StoneBonus1"))
					selectTech("StoneBonus1");
				else
					selectTech("StoneBonus2");
			}
		});

		culture.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectTech("Points");
			}
		});

		war.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(tech.getWarTechLevel()==1) {
					selectTech("War2");
				}
				if(tech.getWarTechLevel()==2) {
					selectTech("War3");
				}
			}
		});

		metal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(MetalL[0] == 1)
					selectTech("MetalW");
				else if(MetalL[1] == 1)
					selectTech("MetalW1");
				else if(MetalL[2] == 1)
					selectTech("MetalW2");
			}
		});

		tanks.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectTech("Tanks");
			}
		});

		can.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectTech("Can");
			}
		});

		horse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectTech("Horse");
			}
		});

		econ.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(tech.selectTech("Econ2"))
					selectTech("Econ2");
				else
					selectTech("Econ3");
			}
		});

		resef.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(tech.selectTech("Resef1"))
					selectTech("Resef1");
				else if(tech.selectTech("Resef2"))
					selectTech("Resef2");
				else
					selectTech("Resef3");
			}
		});

	}
	/**
	 * method to select tech
	 * @param str the string name for selected tech
	 */
	public void selectTech(String str) {
		tech.selectTech(str);
		pane.removeAll();
		pane.add(new JLabel("Tech upgrading for " +p.getName() +"...", JLabel.CENTER));		
		pane.add(new JLabel("Upgrade tech set to " +str, JLabel.CENTER));
		pane.add(new JLabel("(this better be worth it...)", JLabel.CENTER));
		pane.validate();
		pane.repaint();
	}
}